package com.catersystem.zmy.catersystem.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.catersystem.zmy.catersystem.R;
import com.catersystem.zmy.catersystem.bean.BehindBillsBean;
import com.catersystem.zmy.catersystem.finals.FinalUrl;
import com.catersystem.zmy.catersystem.httputils.GetDataUtils;
import com.catersystem.zmy.catersystem.httputils.HttpUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class BehindBillsAdapter extends BaseQuickAdapter<BehindBillsBean, BaseViewHolder> {
    private IGetCookingStatus mIgetCookingStatus;

    public BehindBillsAdapter(@Nullable List<BehindBillsBean> data,IGetCookingStatus igetCookingStatus) {
        super(R.layout.rv_item_behindbills, data);
        this.mIgetCookingStatus = igetCookingStatus;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final BehindBillsBean item) {
        holder.setText(R.id.tv_type, item.getCname()).setText(R.id.tv_table_num, item
                .getId()).setText(R.id.tv_complete_time, item.getTime());
        holder.setGone(R.id.tv_complete_tag, (item.getDetail_completed() != null && item
                .getDetail_completed().size() > 0) ? true : false);//显示完成标签

        RecyclerView rvDishes = holder.getView(R.id.rv_dishes);
        rvDishes.setLayoutManager(new LinearLayoutManager(mContext));
        final CompleteDishesListAdapter dishesAdapter = new CompleteDishesListAdapter(item.getDetail_uncompleted());
        rvDishes.setAdapter(dishesAdapter);

        RecyclerView rvDishesComplete = holder.getView(R.id.rv_dishes_complete);
        rvDishesComplete.setLayoutManager(new LinearLayoutManager(mContext));
        final CompleteDishesListAdapter completeAdapter = new CompleteDishesListAdapter(item.getDetail_completed());
        rvDishesComplete.setAdapter(completeAdapter);

        //将未完成的菜品置为完成
        dishesAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
                Map<String, String> data = new HashMap<>();
                data.put("orgcode", FinalUrl.getOrgCode());
                data.put("detailid", item.getDetail_uncompleted().get(position).getId());
                data.put("status", "2");
                GetDataUtils.getInstance().refreshCookingStatus(data, mContext, new HttpUtils
                        .IHttpResult() {
                    @Override
                    public void onSuccess(String result) {
                        item.getDetail_completed().add(0, item.getDetail_uncompleted().get(position));
                        item.getDetail_uncompleted().remove(position);
                        mIgetCookingStatus.getCookingStatus(1);
                        notifyDataSetChanged();
                    }
                });
            }
        });
        //将完成的菜品置为未完成
        completeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
                Map<String, String> data = new HashMap<>();
                data.put("orgcode", FinalUrl.getOrgCode());
                data.put("detailid", item.getDetail_completed().get(position).getId());
                GetDataUtils.getInstance().refreshCookingStatus(data, mContext, new HttpUtils
                        .IHttpResult() {
                    @Override
                    public void onSuccess(String result) {
                        item.getDetail_uncompleted().add(0, item.getDetail_completed().get(position));
                        item.getDetail_completed().remove(position);
                        mIgetCookingStatus.getCookingStatus(0);
                        notifyDataSetChanged();
                    }
                });
            }
        });
        //添加子item点击事件
        holder.addOnClickListener(R.id.tv_call_out);
        holder.addOnClickListener(R.id.tv_complete);
    }

    public interface IGetCookingStatus {
        void getCookingStatus(int status);
    }
}
